﻿#include "SettingOfChargeMoney.h"
#include "ui_settingofchargemoney.h"

SettingOfChargeMoney::SettingOfChargeMoney(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettingOfChargeMoney)
{
    ui->setupUi(this);
    ui->labelErrorHintMoney->hide();
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    for (int i=0; i<GUN_AMOUNT; i++)
    {
        countdown[i] = new CountDownTime(ui->frameMid);
        countdown[i]->setGeometry(710, 10, 30, 40);
        countdown[i]->mysetText("60");
        connect(this->countdown[i], SIGNAL(CoundDownTimeisOver()), this, SLOT(countdownOver()));
        connect(&Refreash_Thread[i], SIGNAL(Device_is_Error(int)), this, SLOT(Device_Error_or_NotConnect(int)));
        connect(&Refreash_Thread[i], SIGNAL(Device_is_NotConnect(int)), this, SLOT(Device_Error_or_NotConnect(int)));
    }
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(setCountHide(int)));

    QRegExp regExpChargingPower("^((([1-9])|([1-9]\\.\\d\\d))|(([1-9]\\d)|([1-9]\\d\\.\\d\\d))|(([1-9]\\d\\d)|([1-9]\\d\\d\\.\\d\\d)))$");
    QValidator* validatorChargingPower = new QRegExpValidator(regExpChargingPower, 0);
    ui->ChargeMoney->setValidator(validatorChargingPower);
    keyBoardConnect();
}

SettingOfChargeMoney::~SettingOfChargeMoney()
{
    delete ui;
}
void SettingOfChargeMoney::setCountHide(int current)
{
    if(current == settingOfChargeMoneyPage)
    {
        ui->_focus_gun_->setText(QString::number(focus_gun_));
        ui->ChargeMoney->clear();
        if(!countdown[focus_gun_-1]->OneSecound->isActive())
            countdown[focus_gun_-1]->CountDownStart();
        for(int i=0; i<GUN_AMOUNT; i++)
        {
            if(focus_gun_-1 != i)
            {
                countdown[i]->hide();
            }
            else
            {
                countdown[i]->show();
            }
        }
    }
}
void SettingOfChargeMoney::Device_Error_or_NotConnect(int GUN_NO)
{
    countdown[GUN_NO]->CountDownStop();
}

void SettingOfChargeMoney::countdownOver()
{
    /*待完成*/
    CountDownTime* sender_ = static_cast<CountDownTime*>(sender());
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        if(sender_ == countdown[i])
        {
            Refreash_Thread[i].swipCardflag = 0;
        }
    }
}

void SettingOfChargeMoney::on_buttonOk_clicked()
{
    /*待完成*/

    if(!ui->ChargeMoney->text().isEmpty() &&
            ui->ChargeMoney->text().toFloat()*100 > 0 &&
            ui->ChargeMoney->text().toFloat() <= 999.99)
    {
        onStartingCharging(focus_gun_ - 1, 3, 1, 1,
                           (ui->ChargeMoney->text().toFloat()) * 100,
                           focus_gun_, GUN_STATE[focus_gun_ - 1].VINflag);
        emit StartCharging_RefreshChargeInfo(focus_gun_);
        GUN_STATE[focus_gun_-1].Page = chargeWatchingDCPage;
        countdown[focus_gun_-1]->CountDownStop();
    }
    else
    {
        ui->labelErrorHintMoney->show();
        QTimer::singleShot(8000,ui->labelErrorHintMoney,SLOT(hide()));
        ui->ChargeMoney->clear();
    }
}
void SettingOfChargeMoney::on_buttonBack_clicked()
{
    /*待完成*/
    GUN_STATE[focus_gun_-1].Page = chargeTypePage;
    countdown[focus_gun_-1]->CountDownStop();
}

void SettingOfChargeMoney::on_buttonCancelCharging_clicked()
{
    /*待完成*/
    GUN_STATE[focus_gun_-1].Page = returnChargeGunPage;
    countdown[focus_gun_-1]->CountDownStop();
}
void SettingOfChargeMoney::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();

    keyHandle(btn, focus);

}

void SettingOfChargeMoney::keyHandle(QPushButton* btn, QLineEdit* lineEdit)
{
    if( btn->text() == tr("空格") )
    {
        lineEdit->insert(" ");
    }
    else if( btn->text() == tr("退格") )
    {
        lineEdit->backspace();
    }
    else if( btn->text() == tr("清除"))
    {
        lineEdit->clear();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}

void SettingOfChargeMoney::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Space, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
}


